package org.foreverframework.util;

/**
 * 基本数据类型转换工具
 * @author 陈均
 *
 */
public class ConvertUtil {
	/**
	 * 字节数组转化为字符串
	 * @param b 字节数组
	 * @return 转换后的字符串值
	 */
	public static String byteArrayToString(byte[]b){
		if(b==null)return null;
		return new String(b).trim();
	}
	
	/**
	 * 有符号字节转无符号值,对应Byte
	 * 无符号字节0-255
	 * @param b 有符号字节
	 * @return 返回无符号字节值
	 */
	public static short byteToUnsignedByte(byte b){
		if(b<0) return (short)(b & 0xff);
		return b;
	}
	
	/**
	 * 1字节转完整八位二进制字符串
	 * 比如byte = 10转换后00001010
	 * @param b 
	 * @return byte to string
	 */
	public static String byteToBinaryString(byte b){
		String binaryString = Integer.toBinaryString(b&0xff);
		int number = binaryString.length();
		if(number==8)return binaryString;
		return Integer.toBinaryString(((0xff>>number))).replaceAll("1", "0")+binaryString;
	}
	
	/**
	 * 有符号字节数组转无符号short,Word类型
	 * @param bytes 从低位开始
	 * @return 返回无符号short值
	 */
	public static int byteArrayToUnsignedShort(byte[]bytes){
		if(bytes==null)throw new ConvertException("字节数组不能够为null");
		String b_1 = byteToBinaryString(bytes[0]);
		String b_2 = byteToBinaryString(bytes[1]);
		String value = b_2+b_1;
		return Integer.parseInt(value,2);
	}
	
	/**
	 * 无符号2字节类型转字节数组形式
	 * @param s 无符号值
	 * @return 返回以字节数组表示的无符号值
	 */
	public static byte[] unsignedShortToByteArray(int s){
		byte[] bytes = new byte[2];
		if(s<0 || s>65535){
			throw new RuntimeException("无符号字节返回在0-6535");
		}
		String bs = Integer.toBinaryString(s);
		//16位无符号
		int num =16  - bs.length();
		String val = Integer.toBinaryString(1<<num).substring(1)+bs;
		bytes[0] = (byte) Integer.parseInt(val.substring(8,16), 2);//低八位
		bytes[1] = (byte) Integer.parseInt(val.substring(0,8), 2);//高八位
		return bytes;
	}
	
	/**
	 * 将字节数组转换成整数
	 * @param bytes 4个字节长度，按照二进制，从左到右依次8位一个字节
	 * @return 转换后的整数值
	 */
	public static int byteArrayToInt(byte[]bytes){
		String b_1 = byteToBinaryString(bytes[0]);
		String b_2 = byteToBinaryString(bytes[1]);
		String b_3 = byteToBinaryString(bytes[2]);
		String b_4 = byteToBinaryString(bytes[3]);
		String value =b_4 + b_3 + b_2 + b_1;
		//进行判断，如果最高位是1表示是负数
		if(value.startsWith("1")){
			//取反
			value = value.replaceAll("1", "2")
			.replaceAll("0", "3")
			.replaceAll("2", "0")
			.replaceAll("3", "1");
			return(Integer.parseInt(value,2)+1)*-1;
		}
		return Integer.parseInt(value,2);	
	}
	/**
	 * 将字节数组转换成short类型
	 * @param bytes bytes[0]是二进制低八位，bytes[1]是二进制高八位
	 * @return 转化后的short值
	 */
	public static short byteArrayToShort(byte[]bytes){
		String b_1 = byteToBinaryString(bytes[0]);
		String b_2 = byteToBinaryString(bytes[1]);
		String value = b_2+b_1;
		//进行判断，如果最高位是1表示是负数
		if(value.startsWith("1")){
			//取反
			value = value.replaceAll("1", "2")
			.replaceAll("0", "3")
			.replaceAll("2", "0")
			.replaceAll("3", "1");
			return (short) ((Short.parseShort(value,2)+1)*-1);
		}	
		return Short.parseShort(value,2);
	}

	/**
	 * 无符号字节转字节
	 */
	public static byte unsignedByteToByte(short s) {
		if(s<0 || s>255){
			throw new RuntimeException("超出无符号字节有效范围0-255");
		}
		return (byte)s;
	}
	
	/**
	 * 整型值转字节数组形式
	 * @param s 整型值
	 * @return 返回以字节数组表示的整型值
	 */
	public static byte[] intToByteArray(int i){
		byte[] bytes = new byte[4];
		String bs = Integer.toBinaryString(i);
		int num =32  - bs.length();
		String val = Integer.toBinaryString(1<<num).substring(1)+bs;
		bytes[0] = (byte) Integer.parseInt(val.substring(24,32), 2);
		bytes[1] = (byte) Integer.parseInt(val.substring(16,24), 2);
		bytes[2] = (byte) Integer.parseInt(val.substring(8,16), 2);
		bytes[3] = (byte) Integer.parseInt(val.substring(0,8), 2);
		return bytes;
	}
	
	public static byte[] shortToByteArray(short s) {
		byte[] bytes = new byte[2];
		String bs = Integer.toBinaryString(s);
		if(bs.length()>16)bs = bs.substring(16);
		int num =16  - bs.length();
		String val = Integer.toBinaryString(1<<num).substring(1)+bs;
		bytes[0] = (byte) Integer.parseInt(val.substring(8,16), 2);//低八位
		bytes[1] = (byte) Integer.parseInt(val.substring(0,8), 2);//高八位
		return bytes;
	}
}
